package xyf.p04.tree;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * ClassName: P0429_NLevelOrder
 * Description:
 * Author: xyf
 * Date: 9/6/22 10:02 PM
 * Version: 1.0
 **/
public class P0429_NLevelOrder
{
    class Node
    {
        public int val;
        public List<Node> children;

        public Node()
        {
        }

        public Node(int _val)
        {
            val = _val;
        }

        public Node(int _val, List<Node> _children)
        {
            val = _val;
            children = _children;
        }
    }


    public List<List<Integer>> levelOrder(Node root)
    {
        if (root == null)
        {
            return new ArrayList<>();
        }
        List<List<Integer>> result = new ArrayList<>();
        Queue<Node> queue = new LinkedList<>();
        queue.offer(root);
        while (queue.size() > 0)
        {
            int size = queue.size();
            List<Integer> list = new ArrayList<>();
            for (int i = 0; i < size; i++)
            {
                Node node = queue.poll();
                list.add(node.val);
                List<Node> children = node.children;
                for (Node child : children)
                {
                    queue.offer(child);
                }
            }
            result.add(list);

        }
        return result;
    }
}
